package com.haoniu.houses.controller;

import com.haoniu.common.core.utils.poi.ExcelUtil;
import com.haoniu.common.core.utils.uuid.UUID;
import com.haoniu.common.core.web.controller.BaseController;
import com.haoniu.common.core.web.domain.AjaxResult;
import com.haoniu.common.core.web.page.TableDataInfo;
import com.haoniu.common.log.annotation.Log;
import com.haoniu.common.log.enums.BusinessType;
import com.haoniu.common.security.annotation.RequiresPermissions;
import com.haoniu.houses.domain.Result;
import com.haoniu.houses.domain.TbPremisesInfo;
import com.haoniu.houses.service.ITbPremisesInfoService;
import com.haoniu.houses.utils.QiniuUtil;
import com.qiniu.storage.model.DefaultPutRet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 楼盘信息Controller
 *
 * @author ruoyi
 * @date 2022-07-16
 */
@RestController
@RequestMapping("/premises")
@CrossOrigin
public class TbPremisesInfoController extends BaseController
{
    @Autowired
    private ITbPremisesInfoService tbPremisesInfoService;
    @Autowired
    private QiniuUtil qiniuUtil;

    // @Autowired
    // private UploadService uploadService;

    /**
     * 七牛云文件上传
     *
     * @param file 文件
     * @return
     */
    @RequiresPermissions("houses:premises:upload")
    @RequestMapping("/upload")
    @ResponseBody
    public Result upload(MultipartFile file) {


        if (file.isEmpty()) {
            return Result.error("上传文件不能为空");
        }
        try {
            MultipartFile multipartFile = file;
            String originalFilename = multipartFile.getOriginalFilename();
            String fileExtend = originalFilename.substring(originalFilename.lastIndexOf("."));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String yyyyMMddHHmmss = sdf.format(new Date());
            //默认不指定key的情况下，以文件内容的hash值作为文件名
            String fileKey = UUID.randomUUID().toString().replace("-", "") + "-" + yyyyMMddHHmmss + fileExtend;
            Map<String, Object> map = new HashMap<>();
            DefaultPutRet uploadInfo = qiniuUtil.upload(multipartFile.getInputStream(), fileKey);
            map.put("fileName", uploadInfo.key);
            map.put("originName", originalFilename);
            map.put("size", multipartFile.getSize());
            //七牛云文件私有下载地址（看自己七牛云公开还是私有配置）
            //map.put("url", "/linfen/qiniu/private/file/" + uploadInfo.key);
            map.put("url", "/linfen/qiniu/file/" + uploadInfo.key);//七牛云公开下载地址
            // log.info("文件：" + JSONObject.toJSONString(map));
            return Result.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("上传失败");
        }
    }

    /**
     * 查询楼盘信息列表
     */
    @RequiresPermissions("houses:premises:list")
    @GetMapping("/list")
    public TableDataInfo list(TbPremisesInfo tbPremisesInfo)
    {
        startPage();
        List<TbPremisesInfo> list = tbPremisesInfoService.selectTbPremisesInfoList(tbPremisesInfo);
        return getDataTable(list);
    }

    /**
     * 导出楼盘信息列表
     */
    @RequiresPermissions("houses:premises:export")
    @Log(title = "楼盘信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbPremisesInfo tbPremisesInfo)
    {
        List<TbPremisesInfo> list = tbPremisesInfoService.selectTbPremisesInfoList(tbPremisesInfo);
        ExcelUtil<TbPremisesInfo> util = new ExcelUtil<TbPremisesInfo>(TbPremisesInfo.class);
        util.exportExcel(response, list, "楼盘信息数据");
    }

    /**
     * 获取楼盘信息详细信息
     */
    @RequiresPermissions("houses:premises:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tbPremisesInfoService.selectTbPremisesInfoById(id));
    }

    /**
     * 新增楼盘信息
     */
    @RequiresPermissions("houses:premises:add")
    @Log(title = "楼盘信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbPremisesInfo tbPremisesInfo)
    {
        return toAjax(tbPremisesInfoService.insertTbPremisesInfo(tbPremisesInfo));
    }

    /**
     * 修改楼盘信息
     */
    @RequiresPermissions("houses:premises:edit")
    @Log(title = "楼盘信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbPremisesInfo tbPremisesInfo)
    {
        return toAjax(tbPremisesInfoService.updateTbPremisesInfo(tbPremisesInfo));
    }

    /**
     * 删除楼盘信息
     */
    @RequiresPermissions("houses:premises:remove")
    @Log(title = "楼盘信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbPremisesInfoService.deleteTbPremisesInfoByIds(ids));
    }
}
